package com.example.day45.eventbus;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.Toast;

import com.example.day45.R;
import com.example.day45.eventbus.bean.Info;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

/**
 * 第一步注册事件
 * 1.1>EventBus eventBus = EventBus.getDefault();
 * 1.2>eventBus.register(this);
 * 第二步 注册订阅者()
 * 2.1>@Subscriber
 * 2.声明订阅方法 和订阅参数
 * 第三步注销事件
 * 3.1> eventBus.unregister(this);
 */


@EActivity(R.layout.activity_event_bus)
public class EventBusActivity extends AppCompatActivity {
    @ViewById
    Button stipBtn;
    @ViewById
    ViewPager vp;
    private EventBus eventBus = EventBus.getDefault();


    @AfterInject
    public void inject() {
        eventBus.register(this);
    }

    @AfterViews
    public void init() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new Msg1Fragment_());
        fragments.add(new Msg1Fragment_());
        vp.setAdapter(new VpFragmentAdapter(getSupportFragmentManager(), fragments));
    }

    /**
     * 注册订阅者
     *
     * @param msg
     */
    @Subscriber()
    public void getInfo(String msg) {
//        Toast.makeText(EventBusActivity.this, "我可以说衣服不是我的吗!!!", Toast.LENGTH_SHORT).show();
        eventBus.post(0);
    }


    @Subscriber
    public void getInfo(Info info) {
        Toast.makeText(EventBusActivity.this, info.toString(), Toast.LENGTH_SHORT).show();
    }

    @Click(R.id.stip_btn)
    public void onClick() {
        startActivity(new Intent(this, SendActivity_.class));
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        eventBus.unregister(this);
    }
}
